#include "common.h"
#include <stdio.h>

int isLeap(int year)
{
	if(year <= 0)
	{
		return -1;
	}
	return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) ? 1 : 0;
}

int month2day(int year, int month)
{
	int months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
	if(month <= 0 || month > 12 )
	{
		return -1;
	}
	if(month == 2 && isLeap(year) == 1)
	{
		return 29;
	}
	else 
	{
		return months[month - 1];
	}
}

//from 1900
long sumYearsDays(int year)
{
	if(year < 1900)
	{
		return -1;
	}
	long sum = 0;
	int i = 1900;
	for(; i < year; ++i)
	{
		if(isLeap(year) == 1)	
		{
			sum += 366;
		}
		else
		{
			sum += 365;
		}
	}
	return sum;
}

int sumMonthDays(int year, int month)
{
	if(month <= 0 || month > 12 || year < 0)
	{
		return -1;
	}
	int i = 1;
	int sum = 0;
	for(; i < month; ++i)
	{
		sum += month2day(year, i);
	}
	return sum;
}

int monthFirstDay(long days)
{
	if(days < 0)
	{
		return -1;
	}
	printf("days:%ld\n", days);
	return (days + 1) % 7;
}

void showCalender(int year, int month, int day)
{
	int days = monthFirstDay(sumMonthDays(year, month) + sumYearsDays(year));
	int i = 0;
	printf("日\t一\t二\t三\t四\t五\t六\n");//打印星期
	for(i = 0; i < days; ++i)
	{
		printf("\t");
	}
	for(i = 1; i <= month2day(year,month); ++i)
	{
		if(i == day)
		{
			printf("\033[41m%02d\033[m\t", day);
		}
		else
		{
			printf("%02d\t", i);
		}
		++days;
		if((days % 7) == 0)
		{
			printf("\n");
		}
	}
	printf("\n");
}
